﻿using BlazorOrdering.Application;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Extensions.Localization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;



namespace BlazorOrdering.Web.Entry.Pages
{
    public partial class AddMenu
    {
        private string weekText { get; set; }
        private string Trace { get; set; }
        private Message MessageElement { get; set; }

        [Inject] public MessageService MessageService { get; set; }

        //@inject BlazorOrdering.Web.Core.Data.BoMenuService menuService

        [Inject]
        public IBoMenuService menuService { get; set; }

        private BlazorOrdering.Core.BoMenu bomenu;
        private IEnumerable<SelectedItem> Items = new SelectedItem[]
        {
            new SelectedItem ("早餐", "早餐"),
            new SelectedItem ("午餐", "午餐"),
            new SelectedItem ("晚餐", "晚餐") ,
        };

        private List<SelectedItem> Educations { get; set; }


        /// <summary>baise
        /// OnInitialized 方法
        /// </summary>
        protected override void OnInitialized()
        {
            base.OnInitialized();

            // 初始化参数
            //bomenu =  new BlazorOrdering.Core.BoMenu { Day= System.DateTime.Now, Price=1, Type="早餐", Name=""};
            bomenu = new BlazorOrdering.Core.BoMenu();
            bomenu.Day = System.DateTime.Now.Date.AddDays(1);
            //ChangWeek(bomenu.Day );
            meunlist = new List<BlazorOrdering.Core.BoMenu>();

        }

        private Task OnInvalidSubmit1(EditContext context)
        {
            Trace="填写不符合规范，请检查";
            //Trace ="Nmae="+ Model.Name;
            ShowMessage(Trace);
            return Task.CompletedTask;
        }

        private Task OnValidSubmit(EditContext context)
        {
            string result = menuService.AddOne(bomenu);
            if (result=="ok")
            {
                Trace = "保存成功！";
                meunlist.Add(bomenu);
                ShowMessage(Trace);
            }
            else
            {
                Trace = result;
                ShowMessage(Trace);
            }

            bomenu = new BlazorOrdering.Core.BoMenu();
            bomenu.Day = System.DateTime.Now.Date.AddDays(1);

            return Task.CompletedTask;
        }
        private void ShowMessage(string msg)
        {
            MessageElement.SetPlacement(Placement.Top);
            MessageService?.Show(new MessageOption()
            {
                Host = MessageElement,
                Content = msg
            });
        }

        private Task ChangWeek(DateTime? d)
        {
            //TValue??
            bomenu.Day = (DateTime)d;
            weekText = bomenu.Day.ToString("ddd");
            return Task.CompletedTask;
        }



    }
}
